package com.example.easybuy.service;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.dao.UseraddressDao;
import com.example.easybuy.pojo.User;
import com.example.easybuy.pojo.Useraddress;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UseraddressServiceImpl implements UseraddressService{

    private Logger logger=Logger.getLogger(getClass());

    @Autowired
    private UseraddressDao useraddressDao;

    @Autowired
    private RedisTemplate redisTemplate;

    /*
    添加收货地址
     */
    @Override
    public String addUserAddress(Useraddress useraddress) {
        logger.debug("UseraddressServiceImpl-addUserAddress()-useraddress:"+useraddress);
        int addRow=useraddressDao.addUserAddress(useraddress);
        String success=null;
        if (addRow>0){
            success="添加成功";
        }else {
            success="添加失败";
        }
        logger.debug("UseraddressServiceImpl-addUserAddress()-success："+success);
        return success;
    }

    /*
    根据登录用户id查询收货地址
     */
    @Override
    public List<Useraddress> getUserAddressList(int userId) {
        logger.debug("UseraddressServiceImpl-getUserAddressList()-userId:"+userId);
        int id=Integer.valueOf(userId);
        List<Useraddress> useraddressesList=useraddressDao.getUserAddressList(id);
        logger.debug("UseraddressServiceImpl-getUserAddressList()-useraddressesList:"+useraddressesList);
        return useraddressesList;
    }

    /*
    设置默认地址
     */
    @Override
    public String updataDefaultAddress(String id) {
        logger.debug("UseraddressServiceImpl-updataDefaultAddress()-id:"+id);
        int idInt=Integer.valueOf(id);
        int updataRow=useraddressDao.updataDefaultAddress(idInt);
        String success=null;
        if (updataRow>0){
            success="设置默认地址成功";
        }else{
            success="设置默认地址失败";
        }
        return success;
    }

    /*
    根据id删除收货地址
     */
    @Override
    public String deleteAddress(String id) {
        logger.debug("UseraddressServiceImpl-deleteAddress()-id:"+id);
        int idInt=Integer.valueOf(id);
        int deleteRow=useraddressDao.deleteAddress(idInt);
        String success=null;
        if (deleteRow>0){
            success="删除成功";
        }else {
            success="删除失败";
        }
        logger.debug("UseraddressServiceImpl-deleteAddress()-success:"+success);
        return success;
    }

    /*
    当只有一条收货地址时，设置为默认地址
     */
    @Override
    public String updataByUserId(int userId) {
        logger.debug("UseraddressServiceImpl-updataByUserId()-userId:"+userId);
        int updataRow=useraddressDao.updataByUserId(userId);
        String success=null;
        if (updataRow>0){
            success="设置一个默认收货地址成功";
        }else {
            success="设置一个默认收货地址失败";
        }
        return success;
    }

    /*
    修改送货地址
   */
    @Override
    public String updataAddress(Useraddress useraddress) {
        logger.debug("UseraddressServiceImpl-updataAddress()-useraddress:"+useraddress);
        int updataRow=useraddressDao.updataAddress(useraddress);
        String success=null;
        if (updataRow>0){
            success="修改成功";
        }else {
            success="修改失败";
        }
        return success;
    }

    /*
    根据id查询收货地址
     */
    @Override
    public Useraddress getAddressById(String id) {
        logger.debug("UseraddressServiceImpl-getAddressById()-id:"+id);
        int idInt=Integer.valueOf(id);
        Useraddress useraddress=useraddressDao.getAddressById(idInt);
        logger.debug("UseraddressServiceImpl-getAddressById()-useraddress:"+useraddress.toString());
        return useraddress;
    }

    /*
    查询一个登录账号有几个收货地址
     */
    @Override
    public int getAddressCount(int userId) {
        logger.debug("UseraddressServiceImpl-getAddressCount()-userId:"+userId);
        int addressCount=useraddressDao.getAddressCount(userId);
        logger.debug("UseraddressServiceImpl-getAddressCount()-addressCount:"+addressCount);
        return addressCount;
    }

    /*
   把当前账号所有地址设为默认地址：0
    */
    @Override
    public String updataDefultAll() {
        logger.debug("UseraddressServiceImpl-updataDefultAll()...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        System.out.println("UseraddressController-updataDefultAll()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();



        int updateRow=useraddressDao.updataDefultAll(userId);
        String success=null;
        if (updateRow>0){
            success="修改当前用户所有默认收货地址成功";
        }else {
            success="修改当前用户所有默认收货地址失败";
        }
        logger.debug("UseraddressController-updataDefultAll()-success:"+success);
        return success;
    }
}
